package com.zyao89.view.zloading.ball;


import com.zyao89.view.zloading.util.NumCalcUtil;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.utils.Color;
import ohos.app.Context;

/**
 * Created by zyao89 on 2017/3/29.
 * Contact me at 305161066@qq.com or zyao89@gmail.com
 * For more projects: https://github.com/zyao89
 * My Blog: http://zyao89.me
 */
public class IntertwineBuilder extends BaseBallBuilder {
    // 最终阶段
    private static final int FINAL_STATE = 1;
    private float mBallR;
    private Path mPath;
    // 当前动画阶段
    private int mCurrAnimatorState = 0;


    @Override
    protected void initParams(Context context, Color color) {
        mBallR = getAllSize();
        mPath = new Path();
        initPaint(5, color);
        initPoints(mBallR);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        drawBall(canvas);
    }


    /**
     * 绘制小球
     *
     * @param canvas
     */
    private void drawBall(Canvas canvas) {
        canvas.save();
        mPaint.setStyle(Paint.Style.FILLANDSTROKE_STYLE);
        super.drawBall(canvas, mPath, mPaint);
        canvas.restore();
    }

    @Override
    protected void prepareStart(AnimatorValue floatValueAnimator) {

    }

    @Override
    protected void prepareEnd() {

    }

    @Override
    protected void computeUpdateValue(AnimatorValue animation, float animatedValue) {
        switch (mCurrAnimatorState) {
            case 1:
                animation.setCurveType(Animator.CurveType.ACCELERATE);
                for (int i = 0; i < mBallPoints.size(); i++) {
                    CirclePoint circlePoint = mBallPoints.get(i);
                    if (2 <= i && i <= 7) {
                        circlePoint.setOffsetX(-mBallR * NumCalcUtil.subtract(1, animatedValue));
                        circlePoint.setOffsetY(-mBallR * NumCalcUtil.subtract(1, animatedValue));
                    } else {
                        circlePoint.setOffsetX(mBallR * NumCalcUtil.subtract(1, animatedValue));
                        circlePoint.setOffsetY(mBallR * NumCalcUtil.subtract(1, animatedValue));
                    }
                }
                break;
            case 0:
                animation.setCurveType(Animator.CurveType.ACCELERATE);
                for (int i = 0; i < mBallPoints.size(); i++) {
                    CirclePoint circlePoint = mBallPoints.get(i);
                    if (2 <= i && i <= 7) {
                        circlePoint.setOffsetX(-mBallR * (animatedValue));
                        circlePoint.setOffsetY(-mBallR * (animatedValue));
                    } else {
                        circlePoint.setOffsetX(mBallR * (animatedValue));
                        circlePoint.setOffsetY(mBallR * (animatedValue));
                    }
                }
                break;
        }
    }

    @Override
    public void onRepeat(Animator animator) {
        if (++mCurrAnimatorState > FINAL_STATE) { // 还原到第一阶段
            mCurrAnimatorState = 0;
            for (CirclePoint point : mBallPoints) {
                point.setOffsetY(0);
                point.setOffsetX(0);
            }
        }
    }

}
